<?php
namespace app\api\controller\v1;

use app\api\service\TokenService;
use app\api\validate\IDPostiveInt;
use app\api\model\Article as ArticleModel;
use app\api\model\ArticleType as ArticleTypeModel;
use app\api\model\Banner as BannerModel;
use app\api\model\Route as RouteModel;
use app\lib\exception\BaseException;
use app\api\validate\ActicleValidate;
use app\api\validate\ActicleTypeValidate;
use app\api\validate\TypeValidate;
use think\Validate;
use think\Db;

//新闻类
class Article extends Base
{
    //获取所有文章猎豹
    public function get_article_list($page=1, $size = 10,$key='')
    {
        $get = input('get.');
        if(!isset($get['type']) || empty($get['type']))
            $get['type'] = '1';
        if(!isset($get['page']) || empty($get['page']))
            $get['page'] = '1';
        if (isset($get['keywords']) && !empty($get['keywords']))
            $key = trim($get['keywords']);
        $where['is_hidden'] = '0';
        $where['type'] = $get['type'];
        $list =  ArticleModel::getarticleByPage($page,$size,$key,$where);
        if ($list->isEmpty())
        {
            $data = [
                'current_page' => 1,
                'data' => [],
                'total' => $list->total(),
            ];
        }else
            $data = $list->toArray();
        return json($data,201);

    }

    //获取某文章详情
    public function get_article_info($id)
    {
        (new IDPostiveInt)->goCheck();
        return ArticleModel::with(['category_content'])->where(['id'=>$id])->find();
    }

    //添加文章
    public function add()
    {
        $validate = new ActicleValidate();
        $validate->goCheck();
        $post = input('post.');//过滤非验证器中的字段
        if(isset($post['category_id']) && empty($post['category_id']))
            $post['category_id'] == '';
        $post['author'] = TokenService::getAdminName();
        $res = ArticleModel::CreateOne($post);
        if(!$res){
            throw new BaseException(['msg'=>'添加失败']);
        }
        return 1;
    }

    //删除文章
    public function del($id)
    {
        (new IDPostiveInt)->goCheck();
        Db::startTrans();
        try {
            ArticleModel::destroy($id);
            ArticleTypeModel::where('article_id','=',$id)->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();// 回滚事务
            throw new BaseException(['msg' => '删除失败']);
        }
        return 1;

    }

    //添加文章二级类型
    public function add_type()
    {
        $validate = new ActicleTypeValidate();
        $validate->goCheck();
        $post = $validate->getDataByRule(input('post.'));//过滤非验证器中的字段
        $res = ArticleTypeModel::create($post);
        if(!$res){
            throw new BaseException(['msg'=>'添加失败']);
        }
        return 1;
    }

    //获取二级分类
    public function GetArticleType()
    {
        $validate = new TypeValidate();
        $validate->goCheck();
        $get = input('get.');//过滤非验证器中的字段
        return ArticleTypeModel::getListAll($get);
    }

    //删除二级分类
    public function DelArticleType($id)
    {
        (new IDPostiveInt)->goCheck();
        $res = ArticleTypeModel::destroy($id);
        if(!$res){
            throw new BaseException(['msg'=>'删除失败']);
        }else{
            return 1;
        }
    }

    //小程序获取文章信息
    public function get_info($id){
        $res = ArticleModel::with(['category_content'])->where(['id'=>$id])->find();
        return $res;
    }

    //小程序初始化列表数据
    public function get_list(){
        $res['list_one'] = ArticleModel::with(['category_content_one'])->where(['type'=>'1'])->limit(0,4)->select();//美国须知
        $res['list_two'] = RouteModel::limit(0,5)->select();//机票
        $res['list_four'] = ArticleModel::with(['category_content_one'])->where(['type'=>'4'])->limit(0,4)->select();//美国文化
        $res['list_three'] = ArticleModel::with(['category_content_one'])->where(['type'=>'3'])->limit(0,4)->select();//热门目的地
        $res['list_banner'] = BannerModel::limit(0,3)->select();//banner
        return json($res);
    }

    //查询列表
    public function detail_list($type){
        $res = ArticleModel::with(['category_content_one'])->where(['type'=>$type])->select();
        return json($res);
    }
}
